<template> 
    <div>
        <nav class="navbar navbar-light bg-light">
            <a class="navbar-brand" href="#">
                <p>注册</p> 
            </a>
        </nav>
        <el-form :model="ruleForm" :rules="rules" :label-position="labelPosition" ref="ruleForm" label-width="50px" class="demo-ruleForm">
        <el-form-item label="姓名" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即注册</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
        </el-form>
    </div>
        
</template>
<script>
import axios from 'axios'
export default {
    data(){
        return {
            labelPosition:"left",
            msg:'',//消息提示专用
            ruleForm: {
                name: '',
                username:'',
                password:''
            },
            rules: {
                name: [
                    { required: true, message: '请输入姓名', trigger: 'blur' },
                    { min: 2, max:10 , message: '长度在 2 到 10 个字符', trigger: 'blur' }
                ],
                username: [
                    { required: true, message: '请输入账号', trigger: 'change' },
                    { min: 6, max:12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'change' },
                    { min: 6, max:12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods:{
        open() {
            this.$message(this.msg);
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
            if (valid) {
                axios.post('http://localhost:3000/user',{
                    name:this.ruleForm.name,
                    username:this.ruleForm.username,
                    password:this.ruleForm.password
                }).then(res=>{
                    const response=res.data;
                    if(response.status==1){
                        this.msg=response.result;
                        this.open()
                        this.$router.push('/login')
                    }else{
                        this.msg=response.result;
                        this.open()
                    }
                })
            } else {
                console.log('error submit!!');
                return false;
            }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>

<style scoped>
    body{
        width: 100%;
        height: 100%;
        background:#e6e5e5;
    }
    #Login_box{
        width: 90%;
        margin: 1em auto;
        padding: 1em;
        /* background:#fefefe; */
    }
    .header{
        width: 100%;
        background: #fff;
    }
    .navbar-brand{
        display: flex;
        align-items: center;
        color: #397ee4 !important;
    }
    .navbar-brand>p{
        margin-left: 1em;
        margin-bottom: 0;
    }
    .Login_btn{
        width: 100%;
    }
    .Login_btn>button{
        width: 100%;
        border-radius: 1.25em;
        margin-top: 1em;
    }
    .demo-ruleForm{
        padding: 1em;
    }
</style>
